<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class DownloadsController extends BaseController {
    public function index(){
    	$this->assign('page_title','下载管理');
    	$GroupModel = D("Downloads");
    	$items = $GroupModel->select();
    	$this->assign('volist',$items);
    	$this->assign('colNames',json_encode(array('组ID','组别名称')));
    	//$this->assign('')
        $this->display();
    }
    
    public function add(){

        $DataModel = D("NewsCategory");
    	if(!empty($_POST)) {
    		$GroupModel = D("Downloads");
    		
    		import ( '@.ORG.UploadFile' );
    		$upload = new \UploadFile ();
    		// 设置文件大小
    		$upload->maxSize = 3292200;
    		// 设置文件保存规则唯一
    		$upload->saveRule = 'uniqid';
    		// 设置上传文件的格式
    		$upload->allowExts = array ();
    		// 保存路径
    		$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    		$upload->savePath = $root_path.'/www/Public/Uploads/';
    		// 设置需要生成缩略图，仅对图像文件有效
    		$upload->thumb = false;
    		
    		// 上传失败返回错误信息
    		if (! $upload->upload ()) {
    			$this->error ( $upload->getErrorMsg () );
    		} else {
    			// $this->success('上传成功');
    			// 获取上传文件的信息
    			$inf = $upload->getUploadFileInfo ();
    		}

    		$_POST['create_time'] = date('Y-m-d H:i:s');
    		$_POST['origin_name'] = $inf[0]['name'];
    		$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
    		$_POST['savename'] = "http://".FRONT_SITE_NAME.str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];
    		$_POST['filesize'] = $inf[0]['size'];
    		$_POST['is_show'] = 1;
   
    		$data = $GroupModel->create($_POST);
    		$insert_id = $GroupModel->add($data);
    		if($insert_id){
    			$this->success('文档上传成功！',__APP__.'/index.php/Downloads/index/');
    			
    		}
    	}else{

    		$list = $DataModel->where("ntype=11")->select();
    		$this->assign('category_list',$list);
    	    $this->display();
    	}
    }
    
    public function getData(){
    	$DataModel = D("Downloads");
    	$cate = $DataModel->select();
    	$cate_arr = array();
    	foreach($cate as $v){
    		$cate_arr[$v['id']] = $v['name'];
    	}
    	
//    	$DataModel = D(CONTROLLER_NAME);
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->page($page)->limit($limit)->select();

//    	foreach($list as $vo){
//    		$vo['category'] = $cate_arr[$vo['cid']];
//    		$items['rows'] = $vo;
//    	}

        $items['rows'] = $list;
    	$this->ajaxReturn($items);
    }
       
    public function edit(){
    	$DataModel = D("Downloads");
    	if(!empty($_POST)){
    		if(!empty($_FILES['imgfile']['name'])){
    			import ( '@.ORG.UploadFile' );
    			$upload = new \UploadFile ();
    			// 设置文件大小
    			$upload->maxSize = 3292200;
    			// 设置文件保存规则唯一
    			$upload->saveRule = 'uniqid';
    			// 设置上传文件的格式
    			$upload->allowExts = array ();
    			// 保存路径
    			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    			$upload->savePath = $root_path.'/www/Public/Uploads/';
    			// 设置需要生成缩略图，仅对图像文件有效
    			$upload->thumb = false;
    			// 删除原图
    			$upload->thumbRemoveOrigin = false;
    			// 上传失败返回错误信息
    			if (! $upload->upload ()) {
    				$this->error ( $upload->getErrorMsg () );
    			} else {
    				// $this->success('上传成功');
    				// 获取上传文件的信息
    				$inf = $upload->getUploadFileInfo ();
    			}
    		}
    		if(!empty($inf)){
    			//unlink old file
    			$item = $DataModel->where("id=".(int)$_POST['id'])->select();
    			unlink(str_replace("http://".FRONT_SITE_NAME,$root_path."/www",$item[0]['savename']));
    			$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
    				
    			$_POST['savename'] = "http://".FRONT_SITE_NAME.str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];
    			unlink($inf [0] ['savepath'].$item[0]['savename']);
    			 
    			$_POST ['savename'] =  $inf [0] ['savename'];
    			$_POST['create_time'] = date('Y-m-d H:i:s');
    			$_POST['origin_name'] = $inf[0]['name'];
    			$_POST['filesize'] = $inf[0]['size'];
    		}
    		$DataModel->where("down_id=".(int)$_POST['down_id'])->save($_POST);
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>window.location.href='".__APP__."/index.php/Downloads/';</script>";exit;
    		
    	}else{
    		$item = $DataModel->where("down_id=".(int)$_GET['id'])->select();
    		$this->assign('vo',$item[0]);
    		$DataModel = D("NewsCategory");
    		$list = $DataModel->where("ntype=11")->select();
    		$this->assign('category_list',$list);
    		$this->display();
    	}
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D("Downloads");
    		$id = $_POST['id'];
    		$item = $DataModel->where("down_id=".(int)$id)->find();
    		$result = $DataModel->delete((int)$id);
    
    		if($result){
    			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    			unlink(str_replace("http://".FRONT_SITE_NAME,$root_path."/www",$item['savename']));
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }
    
}